<?php

namespace App\Modules\Admin\Entities\Water;

use App\Models\Model;
use App\Modules\Admin\Entities\Member\UsersModel;
use App\Modules\Admin\Entities\Property\WaterCouponModel;
use Carbon\Carbon;

class WaterOrderModel extends Model
{

    protected $table='water_order';
    protected $primaryKey='order_id';
    protected $is_delete=0;




    public function user_info()
    {
        return $this->hasOne(UsersModel::class,'id','user_id');
    }

    public function water_device_info(){
        return $this->hasOne(WaterDeviceModel::class,'water_id','water_id')->select(['water_id','water_title']);
    }

    public function coupon_info()
    {
        return $this->hasOne(WaterCouponModel::class,'coupon_id','coupon_id')->select(['coupon_id','coupon_title','price','give_money','remark']);
    }

    public function getPayTimeAttribute($value): string
    {
        $date = Carbon::parse($value);
        return $date->format('Y-m-d H:i:s');
    }

    public function getWaterOrderByOrderSn($order_sn)
    {
        return $this->where(['order_sn'=>$order_sn])->first();
    }

    public function updateWaterOrderByOrderSn($order_sn,$update)
    {
        return $this->where(['order_sn'=>$order_sn])->update($update);
    }
    public function updateWaterOrderByOrderId($order_id,$update)
    {
        return $this->where(['order_id'=>$order_id])->update($update);
    }

    /**
     * 生成可用的订单流水号
     */

    /**
     * 生成可用的订单流水号
     */
    public function findAvailableNo()
    {
        // 订单流水号前缀
        $prefix = date('YmdHis');
        // 尝试10次生成
        for ($i = 0; $i < 10; $i++) {
            // 随机生成6位数字,左侧不足位补0
            $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
            // 无约束条件查询订单号
            if(!$this->where(['order_sn'=> $no])->exists()){
                return $no;
            }
        }
        return false;
    }

    /**
     * 每天要统计
     * @param $params
     */
    public function getUserScoreDays($params){
        $query = $this->query()->selectRaw("pay_type,sum(amount) AS value, from_unixtime(created_time,'%Y-%m-%d') as times");
        $query = $query->where('is_water',1);
        $query = $query->where('status', 1);
        $query = $query->whereIn('pay_type',['weixin','alipay']);
        $query = $query->where('is_delete', 0);
        $query = $query->whereBetween($this->table . '.created_time', [$params['start_time'], $params['end_time']]);
        $query = $query->groupBy( 'times' );
        return  $query->get()->toArray();
    }


    public function getOrderLists()
    {

        $status_time =  date("Y-m-d H:i:s", strtotime("-2 hour"));
        $end_time =   date("Y-m-d H:i:s", strtotime("-12 hour"));

        $query = $this->query()->where('type','consume');
//        $query = $query->where('is_water',0);
//        $query = $query->where('status', 1);
//        $query = $query->whereIn('pay_type',['weixin','alipay']);
//        $query = $query->where('is_delete', 0);
        $query = $query->whereBetween(  'created_time', [strtotime($status_time), strtotime($end_time)]);
        return  $query->get()->toArray();

    }

}
